package com.lhk.appframework.manager;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

import android.content.Context;
import android.util.Log;
public class ImplManagerMapBaiDu extends BaseManager implements ManagerMap{
	
	
	public LocationClient locationClient;
	public MyLocationListener myLocationListener;
	
	
	private static ImplManagerMapBaiDu instance = null;
	private ImplManagerMapBaiDu(Context context) {
		
		init(context);
	}
	
	public static ImplManagerMapBaiDu getInstance(Context context){
		if(instance == null){
			synchronized (ImplManagerMapBaiDu.class) {
				if(instance == null){
					instance = new ImplManagerMapBaiDu(context);
				}
			}
		}
		return instance;
	}


	public void init(Context context) {
		
		
		InitLocation(context);
	}
	
	
	private void InitLocation(Context context){
		locationClient = new LocationClient(context);
		myLocationListener = new MyLocationListener();
		locationClient.registerLocationListener(myLocationListener);
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
		option.setCoorType("gcj02");//返回的定位结果是百度经纬度，默认值gcj02
		int span=5000;
		option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);
		locationClient.setLocOption(option);
	}
	
	
	
	@Override
	public void startLocation() {
		if(locationClient != null){
			locationClient.start();
		}
	}
	

	@Override
	public void stopLocation() {
		if(locationClient != null){
			locationClient.stop();
		}
		
	}
	

	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			//Receive Location 
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation){
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
				sb.append("\ndirection : ");
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				sb.append(location.getDirection());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				//运营商信息
				sb.append("\noperationers : ");
				sb.append(location.getOperators());
			}
			Log.i("BaiduLocationApiDem", sb.toString());
		}


	}




	
}
